<template>
  <el-card>
    <el-table border style="margin: 20px 0" :data="skuAttr">
      <el-table-column
        label="序号"
        width="80"
        align="center"
        type="index"
      ></el-table-column>
      <el-table-column
        label="名称"
        prop="skuName"
        width="150"
      ></el-table-column>
      <el-table-column
        label="描述"
        prop="skuDesc"
        width="150"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column label="图片" width="150">
        <template #="{ row, $index }">
          <el-image
            :src="row.skuDefaultImg"
            style="width: 100px; height: 100px"
          />
        </template>
      </el-table-column>
      <el-table-column label="重量" prop="weight" width="150"></el-table-column>
      <el-table-column label="价格" prop="price" width="150"></el-table-column>
      <el-table-column label="操作" width="260px" :fixed="false">
        <template #="{ row, $index }">
          <el-button
            size="small"
            :type="row.isSale == 1 ? 'primary' : 'success'"
            :icon="row.isSale == 1 ? 'Top' : 'Bottom'"
            @click="changeSale(row)"
          ></el-button>
          <el-button
            size="small"
            type="primary"
            icon="Edit"
            @click="updateSku"
          ></el-button>
          <el-button
            size="small"
            type="primary"
            icon="InfoFilled"
            @click="viewSku(row.id)"
          ></el-button>
          <!-- 气泡框组件 -->
          <el-popconfirm
            :title="`你确定要删除${row.skuName}?`"
            width="200px"
            @confirm="removeSku(row.id)"
          >
            <template #reference>
              <el-button size="small" type="primary" icon="Delete"></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 6, 9, 11]"
      :background="true"
      layout=" prev, pager, next, jumper,->,sizes,total"
      :total="total"
      @change="getHasSkuData"
    />
    <!-- 抽屉组件 -->
    <el-drawer v-model="show">
      <template #header>
        <h4>查看商品详情</h4>
      </template>
      <template #default>
        <el-row style="margin: 20px 0">
          <el-col :span="6">名称</el-col>
          <el-col :span="18">{{ viewSkuDetails.skuName }}</el-col>
        </el-row>
        <el-row style="margin: 20px 0">
          <el-col :span="6">描述</el-col>
          <el-col :span="18">{{ viewSkuDetails.skuDesc }}</el-col>
        </el-row>
        <el-row style="margin: 20px 0">
          <el-col :span="6">价格</el-col>
          <el-col :span="18">{{ viewSkuDetails.price }}</el-col>
        </el-row>
        <el-row style="margin: 20px 0">
          <el-col :span="6">平台属性</el-col>
          <el-col :span="18">
            <el-tag
              v-for="item in viewSkuDetails.skuAttrValueList"
              :key="item.id"
              style="margin: 5px"
              >{{ item.valueName }}</el-tag
            >
          </el-col>
        </el-row>
        <el-row style="margin: 20px 0">
          <el-col :span="6">销售属性</el-col>
          <el-col :span="18">
            <el-tag
              v-for="item in viewSkuDetails.skuSaleAttrValueList"
              style="margin: 5px"
              >{{ item.saleAttrName }}</el-tag
            >
          </el-col>
        </el-row>
        <el-row style="margin: 20px 0">
          <el-col :span="6">商品图片</el-col>
          <el-col :span="18">
            <!-- 轮播入组件 -->
            <el-carousel :interval="4000" type="card" height="200px">
              <el-carousel-item
                v-for="item in viewSkuDetails.skuImageList"
                :key="item.id"
              >
                <el-image
                  :src="item.imgUrl"
                  style="width: 100%; height: 100%"
                />
              </el-carousel-item>
            </el-carousel>
          </el-col>
        </el-row>
      </template>
    </el-drawer>
  </el-card>
</template>

<script setup lang="ts">
//引入API
import {
  reqHasSkuData,
  reqOnSaleSku,
  reqCancelSaleSku,
  reqGetSkuInfo,
  reqRemoveSku,
} from "@/api/product/sku";
//引入类型
import type { SkuData } from "@/api/product/spu/type";
import { ElMessage } from "element-plus";
import { defineOptions, ref, onMounted, watch, reactive } from "vue";
defineOptions({
  name: "Sku",
});
let currentPage = ref(1); //当前页
let pageSize = ref(3); //每页数据量
let total = ref(0); //总数据
let skuAttr = ref<SkuData[]>([]); //用户保存已有SKU数据的数组
let show = ref<boolean>(false); //抽屉组件的显示与隐藏
let viewSkuDetails = reactive<SkuData>({});
onMounted(() => {
  getHasSkuData();
});
//获取已有的SKU数据
const getHasSkuData = async () => {
  let result = await reqHasSkuData(currentPage.value, pageSize.value);
  // console.log(result);
  if (result.code == 200) {
    total.value = result.data.total;
    skuAttr.value = result.data.records;
  }
};
//监听每页数据的变化
watch([pageSize], () => {
  currentPage.value = 1;
});
//SKU商品上架｜下架点击回调
const changeSale = async (row: SkuData) => {
  //isSale=1:上架 -> 通知为下架
  //isSale=0:下架 -> 通知为上架
  if (row.isSale == 1) {
    await reqCancelSaleSku(row.id as number);
    ElMessage({ type: "success", message: "下架成功" });
    //重新获取数据
    getHasSkuData();
  } else {
    await reqOnSaleSku(row.id as number);
    ElMessage({ type: "success", message: "上架成功" });
    //重新获取数据
    getHasSkuData();
  }
};
//更新已有的SKU
const updateSku = () => {
  ElMessage({ type: "success", message: "程序员正在更新中..." });
};
//查看已有的SKU
const viewSku = async (id: number) => {
  let result = await reqGetSkuInfo(id);
  console.log(result);
  if (result.code == 200) {
    viewSkuDetails = result.data;
    show.value = true;
  }
};
//删除某一个已有的SKU商品
const removeSku = async (id: number) => {
  let result = await reqRemoveSku(id);
  if (result.code == 200) {
    ElMessage({ type: "success", message: "删除成功" });
    getHasSkuData();
  } else {
    ElMessage({ type: "error", message: "系统数据不能删除" });
  }
};
</script>

<style scoped lang="scss">
.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
